
import { Data } from "../../../modeal/data.js"
import { User } from "../../../modeal/user.js"
const data = new Data()
const user = new User()
Page({
  data: {
    userInfo: {},
    rechargeInfo: {},
    focus: false,
    currentIndex: 0,
    inputText: '',
    amount: 0.00, //充值金额

  },
  onLoad: function (options) {
    let userInfo = wx.getStorageSync('userinfo');
    this.setData({ userInfo })
    this.getRechargeConfig();   // 获取充值配置信息
  },

  onReady: function () { },
  onShow: function () { },

  // 获取充值配置信息
  getRechargeConfig () {
    data.getRechargeConfig().then(res => {
      this.setData({ rechargeInfo: res })
    })
  },

  // input 触发focus
  bindfocus (e) {
    this.setData({
      focus: true,
      currentIndex: null
    })
  },

  bindblur (e) {
    // this.data.inputText.length < 1 ? this.setData({ currentIndex: 0 }) : ''
    let inputText = e.detail.value
    let payConfig = this.data.rechargeInfo
    let amount = parseFloat(inputText)

    let min_Amount = this.data.rechargeInfo.min_recharge
    let max_Amount = this.data.rechargeInfo.max_recharge
    this.setData({ focus: false })
    if (inputText == "") {
      this.setData({
        currentIndex: 0,
        amount: payConfig.charging_activity_config[0].money
      })
      return
    }

    if (amount < min_Amount) {
      this.showModal()
      return

    }
    if (amount > max_Amount) {
      this.showModal()
      return
    }

    this.setData({ amount })
  },

  showModal () {
    wx.showModal({
      title: '充值金额',
      content: '充值金额在' + this.data.rechargeInfo.min_recharge + '至' + this.data.rechargeInfo.max_recharge + '之间',
      showCancel: false,
    })
  },

  bindinput (e) {
    this.setData({ inputText: e.detail.value })
  },


  // 选择充值选项 
  clickRecharge (e) {
    let currentIndex = e.currentTarget.dataset.index;
    let amount = this.data.rechargeInfo.charging_activity_config[currentIndex].money
    this.setData({
      currentIndex, amount,
      inputText: '',
    })
  },

  submit () {
    let amount = this.data.amount
    amount ? "" : amount = 100

    // 充值
    user.wxPay(amount).then(res => {
      wx.showModal({
        title: "用于测试",
        content: "本次充值金额为" + amount,
        showCancel: false,
      })

      // 发起支付
      // wx.requestPayment({
      //   timeStamp: String(data.timeStamp),
      //   nonceStr: data.nonceStr,
      //   package: data.package,
      //   signType: data.signType,
      //   paySign: data.paySign,
      //   success: res => {
      //     wx.showModal({
      //       title: '充值提示',
      //       content: '充值成功',
      //       showCancel: false,
      //       success: () => {
      //         wx.navigateBack({
      //           delta: 2
      //         });
      //       }
      //     });
      //   },
      //   fail: () => {
      //     wx.showModal({
      //       title: '充值提示',
      //       content: '充值失败',
      //       showCancel: false,
      //     });
      //   },
      //   complete: (res) => {
      //     // console.log(res);
      //   }
      // })

    })

  }
})